package com.huihui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huihui.R;
import com.huihui.bean.Comment;
import com.huihui.utils.LoadImageUtil;
import com.huihui.widget.PartColorTextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/9/17.
 */

public class ChildCommentAdapter extends RecyclerView.Adapter<ChildCommentAdapter.ChildViewHolder>{

    private Context context;
    private List<Comment> list;

    public ChildCommentAdapter(Context context, List<Comment> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ChildViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chile_comment_item2, parent, false);
        return new ChildViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ChildViewHolder holder, int position) {
        Comment item = list.get(position);
        LoadImageUtil.displayImage(context, item.getUser_head_photo(), holder.headView);
        holder.user_name.setText(item.getUser_nick_name());
        holder.user_time.setText(item.getCom_time());
        int is_Like = item.getIs_like();
        if (is_Like == 1) {
            holder.zanIcon.setImageResource(R.mipmap.zan_press);
        } else {
            holder.zanIcon.setImageResource(R.mipmap.zan);
        }
        holder.zanCount.setText(String.valueOf(item.getCollection_num()));
        holder.comment_content.setText(item.getCom_content());
            holder.item2_layout.setTag(position);
            holder.item2_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int p = (int) v.getTag();
                    if(listener != null) {
                        listener.onClick(p);
                    }
                }
            });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }



    public class ChildViewHolder extends RecyclerView.ViewHolder{
        CircleImageView headView;
        TextView user_name;
        TextView user_time;
        ImageView zanIcon;
        TextView zanCount;
        PartColorTextView comment_content;
        LinearLayout item2_layout;

        public ChildViewHolder(View itemView) {
            super(itemView);
            headView = itemView.findViewById(R.id.head_view);
            user_name = itemView.findViewById(R.id.user_name);
            user_time = itemView.findViewById(R.id.user_time);
            zanIcon = itemView.findViewById(R.id.zanIcon);
            zanCount = itemView.findViewById(R.id.zanCount);
            comment_content = itemView.findViewById(R.id.comment_content);
            item2_layout = itemView.findViewById(R.id.item2_layout);
        }
    }

    private OnChildCommentListener listener;

    public void setOnChildCommentListener(OnChildCommentListener listener) {
        this.listener = listener;
    }

    public interface OnChildCommentListener {
        void onClick(int position);
    }

}
